package com.zatech.cgnci.project.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zatech.cgnci.project.base.context.UserContext;
import com.zatech.cgnci.project.base.enums.ApprovalConfigEnum;
import com.zatech.cgnci.project.base.enums.BusinessScenariosEnum;
import com.zatech.cgnci.project.base.enums.OperationTypeEnum;
import com.zatech.cgnci.project.base.utils.BeanUtils;
import com.zatech.cgnci.project.base.utils.VerifyUtils;
import com.zatech.cgnci.project.dto.*;
import com.zatech.cgnci.project.entity.CgnciReInsuranceCompanyDO;
import com.zatech.cgnci.project.entity.UserEntity;
import com.zatech.cgnci.project.service.CgnciCodeTypeService;
import com.zatech.cgnci.project.service.CgnciOperationLogsService;
import com.zatech.cgnci.project.service.CgnciReInsuranceCompanyService;
import com.zatech.cgnci.project.service.IApprovalService;
import com.zatech.cgnci.project.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 再保公司
 *
 * @author P641108
 */

@RestController
@RequestMapping("/api/insurance-company")
@RequiredArgsConstructor
@Api(produces = "application/json", tags = "再保公司api接口文档")
public class CgnciReInsuranceCompanyController {
    private final CgnciReInsuranceCompanyService reInsuranceCompanyService;

    private final IApprovalService approvalService;

    private final CgnciOperationLogsService cgnciOperationLogsService;

    private final CgnciCodeTypeService cgnciCodeTypeService;


    @PostMapping("/page")
    @ApiOperation(value = "分页列表查询")
    public IPage<CgnciReInsuranceCompanyPageVO> page(@RequestBody CgnciReInsuranceCompanyQueryDTO dto) {
        // 获取当前登录用户
        UserEntity user = UserContext.getUser();
        IPage<CgnciReInsuranceCompanyPageVO> page = reInsuranceCompanyService.pageInsuranceCompany(dto);
        List<CgnciReInsuranceCompanyPageVO> records = page.getRecords();
        if (VerifyUtils.isNotEmpty(records)) {
            records.forEach(record -> record.setHasEdit("3".equals(record.getStatus()) && user.getNameAndCode().equals(record.getCreateUser())));
        }
        return page;
    }

    @GetMapping("/detail")
    @ApiOperation(value = "详情")
    public CgnciReInsuranceCompanyMainVO detail(@RequestParam(value = "id") @NotNull(message = "id不能为空") Long id) {
        return reInsuranceCompanyService.detail(id);
    }

    @PostMapping("/next/create")
    @ApiOperation(value = "新建再保公司")
    @Transactional(rollbackFor = Exception.class)
    public Long nextCreate() {
        // 获取当前登录用户
        UserEntity user = UserContext.getUser();
        // 第一步先插入业务数据,用来绑定工作量formId formNo,并且关联工作台的跳转地址url以及参数
        CgnciReInsuranceCompanyDO reInsuranceCompanyDO = CgnciReInsuranceCompanyDO.builder()
                .formNo(BeanUtils.getUUID())
                .companyCode("C" + cgnciCodeTypeService.getBusinessNo("companyCode", "number"))
                .createUser(user.getNameAndCode())
                .updateUser(user.getNameAndCode()).build();
        reInsuranceCompanyService.save(reInsuranceCompanyDO);


        // 创建任务,绑定工作流formId,formNo 并且关联工作台的跳转地址url以及参数
        ApprovalInfoVO approvalInfoVO = approvalService.create(FlowTaskCreateDTO.builder().approvalConfigGroupId(ApprovalConfigEnum.APPROVAL_CONFIG_003.getGroupId()).formId(String.valueOf(reInsuranceCompanyDO.getId())).formNo(reInsuranceCompanyDO.getFormNo())
                .redirectionUrl(ApprovalConfigEnum.APPROVAL_CONFIG_003.getRedirectionUrl()).redirectionParam(String.valueOf(reInsuranceCompanyDO.getId())).redirectionDesc(ApprovalConfigEnum.APPROVAL_CONFIG_003.getRedirectionDesc()).build());
        cgnciOperationLogsService.insertOperationLogs(
                String.valueOf(reInsuranceCompanyDO.getId()),
                BusinessScenariosEnum.REINSURANCECOMPANY.getCode(),
                OperationTypeEnum.SUBMIT_REINSURANCE_COMPANY.getCode(),
                "新增再保公司任务号：" + approvalInfoVO.getTaskId(),
                user.getNameAndCode()
        );
        return reInsuranceCompanyDO.getId();
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation(value = "新增或者修改")
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(@RequestBody @Valid CgnciReInsuranceCompanySaveOrUpdateDTO saveOrUpdateDTO) {

        CgnciReInsuranceCompanyDO reInsuranceCompanyDO = reInsuranceCompanyService.saveOrUpdateInsuranceCompany(saveOrUpdateDTO);

        // 提交审核
        if (VerifyUtils.isNotEmpty(saveOrUpdateDTO.getFlowTaskType()) && "submit".equals(saveOrUpdateDTO.getFlowTaskType())) {
            reInsuranceCompanyService.submit(FlowTaskSubmitDTO.builder().approvalInfoId(saveOrUpdateDTO.getApprovalInfoId()).formId(String.valueOf(reInsuranceCompanyDO.getId())).build());
        }
    }

    @GetMapping("/next/edit")
    @ApiOperation(value = "编辑再保公司")
    @Transactional(rollbackFor = Exception.class)
    public Long nextEdit(@RequestParam(value = "id") @NotNull(message = "id不能为空") Long id) {
        // 获取当前登录用户
        UserEntity user = UserContext.getUser();
        CgnciReInsuranceCompanyDO reInsuranceCompanyDO = reInsuranceCompanyService.nextEdit(id);

        // 创建任务,绑定工作流formId,formNo 并且关联工作台的跳转地址url以及参数
        ApprovalInfoVO approvalInfoVO = approvalService.create(FlowTaskCreateDTO.builder().approvalConfigGroupId(ApprovalConfigEnum.APPROVAL_CONFIG_004.getGroupId()).formId(String.valueOf(reInsuranceCompanyDO.getId())).formNo(reInsuranceCompanyDO.getFormNo())
                .redirectionUrl(ApprovalConfigEnum.APPROVAL_CONFIG_004.getRedirectionUrl()).redirectionParam(String.valueOf(reInsuranceCompanyDO.getId())).redirectionDesc(ApprovalConfigEnum.APPROVAL_CONFIG_004.getRedirectionDesc()).build());
        cgnciOperationLogsService.insertOperationLogs(
                String.valueOf(reInsuranceCompanyDO.getId()),
                BusinessScenariosEnum.REINSURANCECOMPANY.getCode(),
                OperationTypeEnum.SUBMIT_EDIT_REINSURANCE_COMPANY.getCode(),
                "编辑再保公司任务号：" + approvalInfoVO.getTaskId(),
                user.getNameAndCode()
        );
        return reInsuranceCompanyDO.getId();
    }

    @PostMapping("/approve")
    @ApiOperation(value = "审核流程任务")
    public void approve(@RequestBody @Valid FlowTaskApprovalDTO flowTaskDTO) {
        reInsuranceCompanyService.approve(flowTaskDTO);
    }

    @GetMapping("/listBackNode")
    @ApiOperation(value = "获取退回流程节点")
    public List<ApprovalConfigVO> listBackNode(@RequestParam(value = "id") @NotNull(message = "id不能为空") Long id) {
        CgnciReInsuranceCompanyDO reInsuranceCompany = reInsuranceCompanyService.getById(id);
        return approvalService.listBackNode(String.valueOf(reInsuranceCompany.getId()), reInsuranceCompany.getFormNo());
    }

    @PostMapping("/back")
    @ApiOperation(value = "退回流程任务")
    public void back(@RequestBody @Valid FlowTaskBackDTO flowTaskDTO) {
        reInsuranceCompanyService.back(flowTaskDTO);
    }

    @GetMapping("/signOut")
    @ApiOperation(value = "失效")
    public void signOut(@RequestParam(value = "id") @NotNull(message = "id不能为空") Long id) {
        reInsuranceCompanyService.signOut(id);
    }

    @GetMapping("/log")
    @ApiOperation(value = "获取操作日志")
    public List<ApprovalRecordVO> log(@RequestParam(value = "id") @NotNull(message = "id不能为空") Long id) {
        return reInsuranceCompanyService.log(id);
    }
}

